package cn.ad.server.job.adapter.dbaccess;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.ad.server.common.Constants;
import cn.ad.server.common.Constants.UpdateMode;
import cn.ad.server.common.utils.BeanUtils;
import cn.ad.server.domain.SysConfig;
import cn.ad.server.domain.ad.AdMaterial;
import cn.ad.server.domain.ad.AdMaterialAudit;
import cn.ad.server.domain.ad.AdMaterialGroup;
import cn.ad.server.domain.ad.AdSize;
import cn.ad.server.domain.ad.AdSpace;
import cn.ad.server.domain.ad.AdTemplate;
import cn.ad.server.domain.ad.AdTemplateSpec;
import cn.ad.server.domain.ad.AppClass;
import cn.ad.server.domain.ad.Campaign;
import cn.ad.server.domain.ad.Creative;
import cn.ad.server.domain.ad.DayTargert;
import cn.ad.server.domain.ad.InteractionApp;
import cn.ad.server.domain.ad.InteractionRedirects;
import cn.ad.server.domain.ad.Interest;
import cn.ad.server.domain.ad.Platform;
import cn.ad.server.domain.ad.PlatformAdspaceMap;
import cn.ad.server.domain.ad.PlatformIndustryMap;
import cn.ad.server.domain.ad.PlatformSizeMap;
import cn.ad.server.domain.ad.PlatformTagSizeMap;
import cn.ad.server.domain.ad.PlatformTagTemplateMap;
import cn.ad.server.domain.ad.PubSite;
import cn.ad.server.domain.ad.Ratio;
import cn.ad.server.domain.ad.Strategy;
import cn.ad.server.domain.ad.StrategyCreativeMap;
import cn.ad.server.domain.ad.StrategyCrowdMap;
import cn.ad.server.domain.ad.StrategyDeviceMap;
import cn.ad.server.domain.ad.StrategyGeoMap;
import cn.ad.server.domain.ad.StrategyGroupPlatformMap;
import cn.ad.server.domain.ad.StrategyInterestMap;
import cn.ad.server.domain.ad.StrategyPlatformMap;
import cn.ad.server.domain.ad.StrategySpaceMap;
import cn.ad.server.domain.enumration.AuditStatus;

/**
 * 数据容器
 * 
 */
public class DbDataContainer {
	static final Logger logger = LoggerFactory.getLogger(DbDataContainer.class);

	public static DateTime dbQueryTime;//防止执行时间过长。下次查询根据的时间。
	private static DbContext lastCtx;//最后一次查询的上下文
	private static final UpdateMode UPDATE_MUDO=Constants.UPDATE_MUDO;//更新方式。
	
	public static void updateMerge(DbContext ctx) {
		DbDataContainer.lastCtx=ctx;
		dbQueryTime=ctx.getDbQueryTime();
		for (AdSpace space : ctx.getListAdSpace()) {
			loadAdspaceAll(space);
			if (space.getStatus() == 1) {
				loadAdspace(space);
			} else {
				unloadAdspace(space);
			}
		}
		for (AdMaterialGroup mg : ctx.getListAdMaterialGroup()) {
			loadAdMaterialGroupAll(mg);
			if (mg.getStatus() == 1) {
				loadAdMaterialGroup(mg);
			} else {
				unloadAdMaterialGroup(mg);
			}
		}
		for (AdMaterial adm : ctx.getListAdMaterial()) {			
			loadAdMaterialAll(adm);
			if (adm.getStatus() == 1) {
				loadAdMaterial(adm);
			} else {
				unloadAdMaterial(adm);
			}
		}
		for (InteractionRedirects ir : ctx.getListInteractionRedirects()) {
			loadInteractionRedirects(ir);
		}
		for (InteractionApp ia : ctx.getListInteractionApp()) {
			loadInteractionApp(ia);
		}
		for (AdTemplateSpec adts : ctx.getListAdTemplateSpec()) {
			if (adts.getStatus() == 1) {
				loadAdTemplateSpec(adts);
			} else {
				unloadAdTemplateSpec(adts);
			}
		}
		for (AdTemplate adt : ctx.getListAdTemplate()) {
			if (adt.getStatus() == 1) {
				loadAdTemplate(adt);
			} else {
				unloadAdTemplate(adt);
			}
		}
		for (Ratio rt : ctx.getListRatio()) {
			if (rt.getStatus() == 1) {
				loadRatio(rt);
			} else {
				unloadRatio(rt);
			}
		}
		for (PubSite ps : ctx.getListPubSite()) {
			if (ps.getStatus() == 1) {
				loadPubSite(ps);
			} else {
				unloadPubSite(ps);
			}
		}
		for (Creative ct : ctx.getListCreative()) {
			loadCreativeAll(ct);
			if (ct.getStatus() == 1) {
				loadCreative(ct);
			} else {
				unloadCreative(ct);
			}
		}
		if(ctx.getListAppClass()!=null){
			for (AppClass ac : ctx.getListAppClass()) {
				loadAppClass(ac);
			}
		}
		
		for (AdSize ads : ctx.getListAdSize()) {
			if (ads.getStatus() == 1) {
				loadAdSize(ads);
			} else {
				unloadAdSize(ads);
			}
		}
		
		/**
		 * mapStrategyGroupPlatformMapAll 和  extractStrategyPlatformMap对应。
		 */
		mapStrategyGroupPlatformMapAll.clear();
		
		for (Strategy s : ctx.getListStrategy()) {
			extractStrategyPlatformMap(s);
			loadStrategyAll(s);
			if (s.getStatus() == 1) {
				loadStrategy(s);
			} else {
				unloadStrategy(s);
			}
		}
		
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategySpaceMap.clear();
		}
		for (StrategySpaceMap s : ctx.getListStrategySpaceMap()) {
			if (s.getStatus() == 1) {
				loadStrategySpaceMap(s);
			} else {
				unloadStrategySpaceMap(s);
			}
		}
		for (Campaign c : ctx.getListCampaign()) {
			loadCampaignAll(c);
			if (c.getStatus() == 1) {
				loadCampaign(c);
			} else {
				unloadCampaign(c);
			}
		}
		
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyGeoMap.clear();
		}
		for (StrategyGeoMap s : ctx.getListStrategyGeoMap()) {
			if (s.getStatus() == 1) {
				loadStrategyGeoMap(s);
			} else {
				unloadStrategyGeoMap(s);
			}
		}
		
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyCreativeMap.clear();
		}
		for (StrategyCreativeMap s : ctx.getListStrategyCreativeMap()) {
			if (s.getStatus() == 1) {
				loadStrategyCreativeMap(s);
			} else {
				unloadStrategyCreativeMap(s);
			}
		}
		
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyDayMap.clear();
		}
		for (DayTargert s : ctx.getListStrategyDayMap()) {
			if (s.getStatus() == 1) {
				loadStrategyDayMap(s);
			} else {
				unloadStrategyDayMap(s);
			}
		}
		for (SysConfig c : ctx.getListSysConfig()) {
			if (c.getStatus() == 1) {
				loadSysConfig(c);
			} else {
				unloadSysConfig(c);
			}
		}
		for (AdMaterialAudit a : ctx.getListAdMaterialAudit()) {
			loadAdMaterialAuditAll(a);
			if (a.getStatus() == 1) {
				loadAdMaterialAudit(a);
			} else {
				unloadAdMaterialAudit(a);
			}
		}
		
		for (Platform p : ctx.getListPlatform()) {
			loadPlatformAll(p);
			if (p.getStatus() == 1) {
				loadPlatform(p);
			} else {
				unloadPlatform(p);
			}
		}
		
		for (PlatformIndustryMap pi : ctx.getListPlatformIndustryMap()) {
			loadPlatformIndustryMapAll(pi);
			if (pi.getStatus() == 1) {
				loadPlatformIndustryMap(pi);
			} else {
				unloadPlatformIndustryMap(pi);
			}
		}
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyCrowdMapAll.clear();
			mapStrategyCrowdMap.clear();
		}
		for (StrategyCrowdMap scm : ctx.getListStrategyCrowdMap()) {
			loadStrategyCrowdMapAll(scm);
			if (scm.getStatus() == 1) {
				loadStrategyCrowdMap(scm);
			} else {
				unloadStrategyCrowdMap(scm);
			}
		}
		
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyDeviceMapAll.clear();
			mapStrategyDeviceMap.clear();
		}
		for (StrategyDeviceMap sdm : ctx.getListStrategyDeviceMap()) {
			loadStrategyDeviceMapAll(sdm);
			if (sdm.getStatus() == 1) {
				loadStrategyDeviceMap(sdm);
			} else {
				unloadStrategyDeviceMap(sdm);
			}
		}
		
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyInterestMapAll.clear();
			mapStrategyInterestMap.clear();
		}
		for (StrategyInterestMap sim : ctx.getListStrategyInterestMap()) {
			loadStrategyInterestMapAll(sim);
			if (sim.getStatus() == 1) {
				loadStrategyInterestMap(sim);
			} else {
				unloadStrategyInterestMap(sim);
			}
		}
		if (UPDATE_MUDO==UpdateMode.physicsMode) {
			mapStrategyPlatformMapAll.clear();
			mapStrategyIdsByPlatformMap.clear();
			mapStrategyPlatformMap.clear();
		}
		for (StrategyPlatformMap spm : ctx.getListStrategyPlatformMap()) {
			loadStrategyPlatformMapAll(spm);
			if (spm.getStatus() == 1) {
				loadStrategyPlatformMap(spm);
			} else {
				unloadStrategyPlatformMap(spm);
			}
		}
		for (PlatformSizeMap ps : ctx.getListPlatformSizeMap()) {
			loadPlatformSizeMapAll(ps);
			if (ps.getStatus() == 1) {
				loadPlatformSizeMap(ps);
			} else {
				unloadPlatformSizeMap(ps);
			}
		}
		
		for (Interest i : ctx.getListInterest()) {
			//if (i.getStatus() == 1) {
				loadInterest(i);
			//} else {
			//	unloadInterest(i);
			//}
		}
		
		for (PlatformTagTemplateMap ptm : ctx.getListPlatformTagTemplateMap()) {
			loadPlatformTagTemplateMapAll(ptm);
			if (ptm.getStatus() == 1) {
				loadPlatformTagTemplateMap(ptm);
			} else {
				unloadPlatformTagTemplateMap(ptm);
			}
		}
		for (PlatformTagSizeMap psm : ctx.getListPlatformTagSizeMap()) {
			loadPlatformTagSizeMapAll(psm);
			if (psm.getStatus() == 1) {
				loadPlatformTagSizeMap(psm);
			} else {
				unloadPlatformTagSizeMap(psm);
			}
		}
		for (PlatformAdspaceMap psm : ctx.getListPlatformAdspaceMap()) {
			loadPlatformAdspaceMapAll(psm);
			if (psm.getStatus() == 1) {
				loadPlatformAdspaceMap(psm);
			} else {
				unloadPlatformAdspaceMap(psm);
			}
		}
	}


	private static ConcurrentHashMap<String, String> mapSysConfigKVs = new ConcurrentHashMap<>();
	
	private static ConcurrentHashMap<String, SysConfig> mapSysConfig = new ConcurrentHashMap<>();
	
	public static Map<String, String> getSysConfigValues() {
		Set<String> keySet = mapSysConfigKVs.keySet();
		Map<String,String> copy = new HashMap<>();
		for (String key:keySet) {
			copy.put(new String(key), new String(mapSysConfigKVs.get(key)));
		}
		return copy;
	}
	
	public static String getSysConfigByKeyRefer(String key){
		return mapSysConfigKVs.get(key);
	}
	
	public static List<SysConfig> getSysConfigs() {
		return BeanUtils.mapList(mapSysConfig.values(), SysConfig.class);
	}
	
	private static void loadSysConfig(SysConfig config) {
		String key = config.getConfigKey();
		mapSysConfig.put(key,config);
		mapSysConfigKVs.put(key,config.getConfigValue());
	}
	private static void unloadSysConfig(SysConfig config) {
		String key = config.getConfigKey();
		mapSysConfig.remove(key);
		mapSysConfigKVs.remove(key);
	}
	
	private static ConcurrentHashMap<Long, CopyOnWriteArraySet<DayTargert>> mapStrategyDayMap = new ConcurrentHashMap<>();
	
	public static List<DayTargert> getDayTargerts(long strategyId) {
		return BeanUtils.mapList(mapStrategyDayMap.get(strategyId), DayTargert.class);
	}
	private static void loadStrategyDayMap(DayTargert s) {
		long strategyId = s.getStrategyId();
		mapStrategyDayMap.putIfAbsent(strategyId, new CopyOnWriteArraySet<DayTargert>());
		mapStrategyDayMap.get(strategyId).remove(s);
		mapStrategyDayMap.get(strategyId).add(s);
	}
	private static void unloadStrategyDayMap(DayTargert s) {
		long strategyId = s.getStrategyId();
		mapStrategyDayMap.putIfAbsent(strategyId, new CopyOnWriteArraySet<DayTargert>());
		mapStrategyDayMap.get(strategyId).remove(s);
	}

	private static ConcurrentHashMap<Long, CopyOnWriteArraySet<Long>> mapStrategyCreativeMap = new ConcurrentHashMap<>();
	
	public static List<Long> getCreativeIds(long strategyId) {
		return BeanUtils.mapLongList(mapStrategyCreativeMap.get(strategyId));
	}
	private static void loadStrategyCreativeMap(StrategyCreativeMap s) {
		long strategyId = s.getStrategyId();
		mapStrategyCreativeMap.putIfAbsent(strategyId, new CopyOnWriteArraySet<Long>());
		mapStrategyCreativeMap.get(strategyId).add(s.getCreativeId());
	}
	
	private static void unloadStrategyCreativeMap(StrategyCreativeMap s) {
		long strategyId = s.getStrategyId();
		mapStrategyCreativeMap.putIfAbsent(strategyId, new CopyOnWriteArraySet<Long>());
		mapStrategyCreativeMap.get(strategyId).remove(Long.valueOf(s.getCreativeId()));
	}

	private static ConcurrentHashMap<Long, CopyOnWriteArraySet<StrategyGeoMap>> mapStrategyGeoMap = new ConcurrentHashMap<>();
	
	public static List<StrategyGeoMap> getStrategyGeoMaps(long strategyId) {
		return BeanUtils.mapList(mapStrategyGeoMap.get(strategyId), StrategyGeoMap.class);
	}
	private static void loadStrategyGeoMap(StrategyGeoMap s) {
		long strategyId = s.getStrategyId();
		mapStrategyGeoMap.putIfAbsent(strategyId, new CopyOnWriteArraySet<StrategyGeoMap>());
		mapStrategyGeoMap.get(strategyId).remove(s);
		mapStrategyGeoMap.get(strategyId).add(s);
	}
	private static void unloadStrategyGeoMap(StrategyGeoMap s) {
		long strategyId = s.getStrategyId();
		mapStrategyGeoMap.putIfAbsent(strategyId, new CopyOnWriteArraySet<StrategyGeoMap>());
		mapStrategyGeoMap.get(strategyId).remove(s);
	}

	private static ConcurrentHashMap<Integer, Campaign> mapCampaignAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Integer, Campaign> mapCampaign = new ConcurrentHashMap<>();
	
	public static List<Campaign> getCampaignAll() {
		return BeanUtils.mapList(mapCampaignAll.values(), Campaign.class);
	}
	public static Campaign getCampaign(Integer campaignId) {
		return BeanUtils.map(mapCampaign.get(campaignId), Campaign.class);
	}
	
	public static Campaign getCampaignRefer(Integer campaignId) {
		return mapCampaign.get(campaignId);
	}
	
	private static void loadCampaignAll(Campaign c) {
		mapCampaignAll.put(c.getId(), c);
	}
	private static void loadCampaign(Campaign c) {
		mapCampaign.put(c.getId(), c);
	}
	private static void unloadCampaign(Campaign c) {
		mapCampaign.remove(c.getId());
	}

	private static ConcurrentHashMap<Long, CopyOnWriteArraySet<Long>> mapStrategySpaceMap = new ConcurrentHashMap<>();
	public static List<Long> getStrategyIds(long spaceId) {
		return BeanUtils.mapLongList(mapStrategySpaceMap.get(spaceId));
	}
	private static void loadStrategySpaceMap(StrategySpaceMap s) {
		Long spaceId = s.getSpaceId();
		mapStrategySpaceMap.putIfAbsent(spaceId, new CopyOnWriteArraySet<Long>());
		mapStrategySpaceMap.get(spaceId).add(s.getStrategyId());
	}
	private static void unloadStrategySpaceMap(StrategySpaceMap s) {
		long spaceId = s.getSpaceId();
		mapStrategySpaceMap.putIfAbsent(spaceId, new CopyOnWriteArraySet<Long>());
		mapStrategySpaceMap.get(spaceId).remove(Long.valueOf(s.getStrategyId()));
	}

	private static ConcurrentHashMap<Long, Strategy> mapStrategyAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, Strategy> mapStrategy = new ConcurrentHashMap<>();//有效的
	
	
	private static ConcurrentHashMap<Integer, StrategyPlatformMap> mapStrategyGroupPlatformMapAll = new ConcurrentHashMap<>();
	
	
	public static ConcurrentHashMap<Long, Strategy> getStrategyMapAll() {
		return mapStrategyAll;
	}
	
	public static Strategy getStrategy(Long strategyId) {
		return BeanUtils.map(mapStrategy.get(strategyId), Strategy.class);
	}
	public static Strategy getStrategyFromAll(Long strategyId) {
		return BeanUtils.map(mapStrategyAll.get(strategyId), Strategy.class);
	}
	public static List<Strategy> getStrategyAll() {
		return BeanUtils.mapList(mapStrategyAll.values(), Strategy.class);
	}
	private static void loadStrategyAll(Strategy s) {
		
		mapStrategyAll.put(s.getId(), s);
	}

	/**
	 * 从Strategy所属的策略组对象中获取StrategyPlatformMap关系。
	 * @param s
	 */
	private static void extractStrategyPlatformMap(Strategy s) {
		if(s!=null&&s.getStrategyGroup()!=null){
			List<StrategyGroupPlatformMap> strategyGroupPlatformMaps = s.getStrategyGroup().getStrategyGroupPlatformMaps();
			if (strategyGroupPlatformMaps!=null) {
				for (StrategyGroupPlatformMap strategyGroupPlatformMap : strategyGroupPlatformMaps) {
					StrategyPlatformMap strategyPlatformMap = new StrategyPlatformMap();
					strategyPlatformMap.setPlatformId(strategyGroupPlatformMap.getPlatformId());
					strategyPlatformMap.setStrategyId(s.getId());
					strategyPlatformMap.setStatus(strategyGroupPlatformMap.getStatus());
					mapStrategyGroupPlatformMapAll.put(strategyGroupPlatformMap.getId(), strategyPlatformMap);
				}
			}
		}
	}

	/**
	 * 获取 StrategyGroupPlatformMapAll所有的值。
	 * @return
	 */
	public static List<StrategyPlatformMap> getStrategyGroupPlatformMapAll() {
		return BeanUtils.mapList(mapStrategyGroupPlatformMapAll.values(), StrategyPlatformMap.class);
	}
	
	private static void loadStrategy(Strategy s) {
		mapStrategy.put(s.getId(), s);
	}
	private static void unloadStrategy(Strategy s) {
		mapStrategy.remove(s.getId());
	}

	private static ConcurrentHashMap<Integer, AdSize> mapAdSize = new ConcurrentHashMap<>();
	
	public static AdSize getAdSize(Integer adSizeId) {
		return BeanUtils.map(mapAdSize.get(adSizeId), AdSize.class);
	}
	private static void loadAdSize(AdSize ads) {
		mapAdSize.put(ads.getId(), ads);
	}
	private static void unloadAdSize(AdSize ads) {
		mapAdSize.remove(ads.getId());
	}

	private static ConcurrentHashMap<Integer, AppClass> mapAppClass = new ConcurrentHashMap<>();
	
	public static AppClass getAppClass(Integer appClassId) {
		return BeanUtils.map(mapAppClass.get(appClassId), AppClass.class);
	}
	private static void loadAppClass(AppClass ac) {
		mapAppClass.put(ac.getId(), ac);
	}
	private static void unloadAppClass(AppClass ac){
		mapAppClass.remove(ac.getId());
	}

	private static ConcurrentHashMap<Long, Creative> mapCreative = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, Creative> mapCreativeAll = new ConcurrentHashMap<>();
	
	public static Creative getCreative(Long creativeId) {
		return BeanUtils.map(mapCreative.get(creativeId), Creative.class);
	}
	public static Creative getCreativeForAll(Long creativeId) {
		return BeanUtils.map(mapCreativeAll.get(creativeId), Creative.class);
	}
	
	public static List<Creative> getCreativeAll() {
		return BeanUtils.mapList(mapCreativeAll.values(), Creative.class);
	}
	
	private static void loadCreativeAll(Creative ct) {
		mapCreativeAll.put(ct.getId(), ct);
	}
	
	private static void loadCreative(Creative ct) {
		mapCreative.put(ct.getId(), ct);
	}
	private static void unloadCreative(Creative ct) {
		mapCreative.remove(ct.getId());
	}

	private static ConcurrentHashMap<Integer, PubSite> mapPubSite = new ConcurrentHashMap<>();
	
	public static PubSite getPubSite(Integer pubSiteId) {
		return BeanUtils.map(mapPubSite.get(pubSiteId), PubSite.class);
	}
	private static void loadPubSite(PubSite ps) {
		mapPubSite.put(ps.getId(), ps);
	}
	private static void unloadPubSite(PubSite ps) {
		mapPubSite.remove(ps.getId());
	}

	private static ConcurrentHashMap<Integer, Ratio> mapRatio = new ConcurrentHashMap<Integer, Ratio>();
	public static Ratio getRatio(Integer ratioId) {
		return BeanUtils.map(mapRatio.get(ratioId), Ratio.class);
	}
	
	private static void loadRatio(Ratio rt) {
		mapRatio.put(rt.getId(), rt);
	}
	
	private static void unloadRatio(Ratio rt) {
		mapRatio.remove(rt.getId());
	}

	private static ConcurrentHashMap<Integer, AdTemplate> mapAdTemplate = new ConcurrentHashMap<Integer, AdTemplate>();
	
	public static AdTemplate getAdTemplate(Integer adTemplateId) {
		return BeanUtils.map(mapAdTemplate.get(adTemplateId), AdTemplate.class);
	}
	private static void loadAdTemplate(AdTemplate adt) {
		mapAdTemplate.put(adt.getId(), adt);
	}
	private static void unloadAdTemplate(AdTemplate adt) {
		mapAdTemplate.remove(adt.getId());
	}

	private static ConcurrentHashMap<Integer, AdTemplateSpec> mapAdTemplateSpec = new ConcurrentHashMap<Integer, AdTemplateSpec>();

	public static AdTemplateSpec getAdTemplateSpec(Integer adTemplateSpecId) {
		return BeanUtils.map(mapAdTemplateSpec.get(adTemplateSpecId), AdTemplateSpec.class);
	}
	
	public static AdTemplateSpec getAdTemplateSpecRefer(Integer adTemplateSpecId) {
		return mapAdTemplateSpec.get(adTemplateSpecId);
	}
	
	private static void loadAdTemplateSpec(AdTemplateSpec adts) {
		mapAdTemplateSpec.put(adts.getId(), adts);
	}
	private static void unloadAdTemplateSpec(AdTemplateSpec adts) {
		mapAdTemplateSpec.remove(adts.getId());
	}

	private static ConcurrentHashMap<Long, InteractionApp> mapInteractionApp = new ConcurrentHashMap<>();

	public static InteractionApp getInteractionApp(Long interAppId) {
		return BeanUtils.map(mapInteractionApp.get(interAppId), InteractionApp.class);
	}
	
	private static void loadInteractionApp(InteractionApp ia) {
		mapInteractionApp.put(ia.getId(), ia);
	}

	private static void unloadInteractionApp(InteractionApp ia) {
		mapInteractionApp.remove(ia.getId());
	}

	private static ConcurrentHashMap<Long, InteractionRedirects> mapInteractionRedirects = new ConcurrentHashMap<>();
	
	public static InteractionRedirects getInteractionRedirect(Long interRediractId) {
		return BeanUtils.map(mapInteractionRedirects.get(interRediractId), InteractionRedirects.class);
	}
	
	private static void loadInteractionRedirects(InteractionRedirects ir) {
		mapInteractionRedirects.put(ir.getId(), ir);
	}

	private static void unloadInteractionRedirects(InteractionRedirects ir) {
		mapInteractionRedirects.remove(ir.getId());
	}

	private static ConcurrentHashMap<Long, AdMaterial> mapAdMaterialAll = new ConcurrentHashMap<>();//所有
	private static ConcurrentHashMap<Long, AdMaterial> mapAdMaterial = new ConcurrentHashMap<>();//有效的
	private static ConcurrentHashMap<String, ConcurrentHashMap<Long, AdMaterial>> mapAdMaterialByCreativeId = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<String, CopyOnWriteArraySet<AdMaterial>> mapAdMaterialListByCreativeId = new ConcurrentHashMap<>();
	
	public static List<AdMaterial> getAdMaterials(long creativeId) {
		return BeanUtils.mapList(mapAdMaterialListByCreativeId.get(getKeyForAdMaterial(creativeId)), AdMaterial.class);
	}
	
	public static List<AdMaterial> getAdMaterialAll() {
		return BeanUtils.mapList(mapAdMaterialAll.values(), AdMaterial.class);
	}
	
	public static AdMaterial getAdMaterial(Long adMaterialId) {
		return BeanUtils.map(mapAdMaterial.get(adMaterialId), AdMaterial.class);
	}
	
	private static void loadAdMaterialAll(AdMaterial adm) {
		mapAdMaterialAll.put(adm.getId(), adm);
	}
	
	private static void loadAdMaterial(AdMaterial adm) {
		String key = getKeyForAdMaterial(adm.getCreativeId());
		mapAdMaterialByCreativeId.putIfAbsent(key, new ConcurrentHashMap<Long, AdMaterial>());
		mapAdMaterialListByCreativeId.putIfAbsent(key, new CopyOnWriteArraySet<AdMaterial>());
		mapAdMaterialByCreativeId.get(key).put(adm.getId(), adm);
		mapAdMaterialListByCreativeId.get(key).remove(adm);
		mapAdMaterialListByCreativeId.get(key).add(adm);
		mapAdMaterial.put(adm.getId(), adm);
	}

	private static void unloadAdMaterial(AdMaterial adm) {
		String key = getKeyForAdMaterial(adm.getCreative().getId());
		mapAdMaterialByCreativeId.putIfAbsent(key, new ConcurrentHashMap<Long, AdMaterial>());
		mapAdMaterialListByCreativeId.putIfAbsent(key, new CopyOnWriteArraySet<AdMaterial>());
		mapAdMaterialByCreativeId.get(key).remove(adm.getId());
		mapAdMaterialListByCreativeId.get(key).remove(adm);
		if (mapAdMaterialByCreativeId.get(key).isEmpty()) {
			mapAdMaterialByCreativeId.remove(key);
		}
		mapAdMaterial.remove(adm.getId());
	}
	public static String getKeyForAdMaterial(long creativeId) {
		String key = "cId:" + creativeId;
		return key;
	}

	private static ConcurrentHashMap<Long,AdMaterialGroup> mapAdMaterialGroupAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<String,CopyOnWriteArraySet<AdMaterialGroup>> mapAdMaterialGroupSet = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<String, ConcurrentHashMap<Long, AdMaterialGroup>> mapAdMaterialGroup = new ConcurrentHashMap<>();
	
	public static List<AdMaterialGroup> getAdMaterialGroups(long creativeId,int adTemplateId) {
		return BeanUtils.mapList(mapAdMaterialGroupSet.get(getKeyForMaterialGroup(creativeId,adTemplateId)), AdMaterialGroup.class);
	}
	public static List<AdMaterialGroup> getAdMaterialGroupAll() {
		return BeanUtils.mapList(mapAdMaterialGroupAll.values(), AdMaterialGroup.class);
	}
	public static List<AdMaterialGroup> getAdMaterialGroups2(int creativeId,int adTemplateId) {
		return BeanUtils.mapList(mapAdMaterialGroup.get(getKeyForMaterialGroup(creativeId,adTemplateId)).values(), AdMaterialGroup.class);
	}
	public static List<AdMaterialGroup> getAdMaterialGroups(String key) {
		return BeanUtils.mapList(mapAdMaterialGroup.get(key).values(), AdMaterialGroup.class);
	}
	public static Set<String> getAdMaterialGroupKeys() {
		return mapAdMaterialGroup.keySet();
	}
	
	private static void loadAdMaterialGroupAll(AdMaterialGroup mg) {
		mapAdMaterialGroupAll.put(mg.getId(), mg);
	}
	
	private static void loadAdMaterialGroup(AdMaterialGroup mg) {
		String key = getKeyForMaterialGroup(mg.getCreative().getId(), mg.getAdTemplate().getId());
		mapAdMaterialGroup.putIfAbsent(key, new ConcurrentHashMap<Long, AdMaterialGroup>());
		mapAdMaterialGroup.get(key).put(mg.getId(), mg);
		mapAdMaterialGroupSet.putIfAbsent(key, new CopyOnWriteArraySet<AdMaterialGroup>());
		mapAdMaterialGroupSet.get(key).remove(mg);
		mapAdMaterialGroupSet.get(key).add(mg);
	}

	private static void unloadAdMaterialGroup(AdMaterialGroup mg) {
		//String key = "cId:" + mg.getCreative().getId() + "tId:" + mg.getAdTemplate().getId();
		String key = getKeyForMaterialGroup(mg.getCreative().getId(), mg.getAdTemplate().getId());
		mapAdMaterialGroup.putIfAbsent(key, new ConcurrentHashMap<Long, AdMaterialGroup>());
		mapAdMaterialGroup.get(key).remove(mg.getId());
		mapAdMaterialGroupSet.putIfAbsent(key, new CopyOnWriteArraySet<AdMaterialGroup>());
		mapAdMaterialGroupSet.get(key).remove(mg);
		if (mapAdMaterialGroup.get(key).isEmpty()) {
			mapAdMaterialGroup.remove(key);
		}
	}
	
	public static String getKeyForMaterialGroup(long creativeId,int adTemplateId) {
		String key = "cId:" + creativeId + "tId:" + adTemplateId;
		return key;
	}

	/**
	 * ad_space 广告位信息 key: ad space Id
	 */
	private static ConcurrentHashMap<Long, AdSpace> mapAdspaceAll = new ConcurrentHashMap<>();//所有
	private static ConcurrentHashMap<Long, AdSpace> mapAdspace = new ConcurrentHashMap<>();//有效
	
	public static List<AdSpace> getAdspaceAll() {
		return BeanUtils.mapList(mapAdspaceAll.values(), AdSpace.class);
	}
	
	public static AdSpace getAdspace(Integer adspaceId) {
		return BeanUtils.map(mapAdspace.get(adspaceId), AdSpace.class);
	}
	
	private static void loadAdspaceAll(AdSpace space) {
		mapAdspaceAll.put(space.getId(), space);
	}
	private static void loadAdspace(AdSpace space) {
		mapAdspace.put(space.getId(), space);
	}
	
	private static void unloadAdspace(AdSpace space) {
		mapAdspace.remove(space.getId());
	}
	
	private static ConcurrentHashMap<Long, AdMaterialAudit> mapAdMaterialAuditAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, AdMaterialAudit> mapAdMaterialAudit = new ConcurrentHashMap<>();
	/**
	 * 以audit.getPlatformId()+audit.getAuditId()为key
	 */
	private static ConcurrentHashMap<String, AdMaterialAudit> mapAdMaterialAuditPassed = new ConcurrentHashMap<>();
	
	public static List<AdMaterialAudit> getAdMaterialAuditAll() {
		return BeanUtils.mapList(mapAdMaterialAuditAll.values(), AdMaterialAudit.class);
	}
	public static AdMaterialAudit getAdMaterialAuditPassedRefer(String key) {
		return mapAdMaterialAuditPassed.get(key);
	}
	
	private static void loadAdMaterialAuditAll(AdMaterialAudit audit) {
		mapAdMaterialAuditAll.put(audit.getId(), audit);
	}
	
	private static void loadAdMaterialAudit(AdMaterialAudit audit) {
		mapAdMaterialAudit.put(audit.getId(), audit);
		if(audit.getAuditStatus()==AuditStatus.SUCCESS.getValue()){			
			mapAdMaterialAuditPassed.put(audit.getPlatformId()+audit.getAuditId(), audit);
		}else{
			mapAdMaterialAuditPassed.remove(audit.getPlatformId()+audit.getAuditId());
		}
	}
	
	private static void unloadAdMaterialAudit(AdMaterialAudit audit) {
		mapAdMaterialAudit.remove(audit.getId());
		mapAdMaterialAuditPassed.remove(audit.getPlatformId()+audit.getAuditId());
	}
	private static ConcurrentHashMap<Integer, Platform> mapPlatformAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Integer, Platform> mapPlatform = new ConcurrentHashMap<>();
	
	public static List<Platform> getPlatformAll() {
		return BeanUtils.mapList(mapPlatformAll.values(), Platform.class);
	}
	public static ConcurrentHashMap<Integer, Platform> getPlatformMap() {
		return mapPlatform;
	}
	public static Platform getPlatform(Integer pId) {
		return BeanUtils.map(mapPlatformAll.get(pId), Platform.class);
	}
	private static void loadPlatformAll(Platform p) {
		mapPlatformAll.put(p.getId(), p);
	}
	private static void loadPlatform(Platform p) {
		mapPlatform.put(p.getId(), p);
	}
	private static void unloadPlatform(Platform p) {
		mapPlatform.remove(p.getId());
	}
	
	private static ConcurrentHashMap<Integer, PlatformIndustryMap> mapPlatformIndustryMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Integer, PlatformIndustryMap> mapPlatformIndustryMap = new ConcurrentHashMap<>();
	private static void loadPlatformIndustryMapAll(PlatformIndustryMap pi) {
		mapPlatformIndustryMapAll.put(pi.getId(), pi);
	}
	private static void loadPlatformIndustryMap(PlatformIndustryMap pi) {
		mapPlatformIndustryMap.put(pi.getId(), pi);
	}
	private static void unloadPlatformIndustryMap(PlatformIndustryMap pi) {
		mapPlatformIndustryMap.remove(pi.getId());
	}
	private static ConcurrentHashMap<Integer, PlatformSizeMap> mapPlatformSizeMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Integer, PlatformSizeMap> mapPlatformSizeMap = new ConcurrentHashMap<>();
	
	private static void loadPlatformSizeMapAll(PlatformSizeMap ps) {
		mapPlatformSizeMapAll.put(ps.getId(), ps);
	}
	private static void loadPlatformSizeMap(PlatformSizeMap ps) {
		mapPlatformSizeMap.put(ps.getId(), ps);
	}
	private static void unloadPlatformSizeMap(PlatformSizeMap ps) {
		mapPlatformSizeMap.remove(ps.getId());
	}
	private static ConcurrentHashMap<Integer, Interest> mapInterest = new ConcurrentHashMap<>();
	private static void loadInterest(Interest interest) {
		mapInterest.put(interest.getId(), interest);
	}
	private static void unloadInterest(Interest interest) {
		mapInterest.remove(interest.getId());
	}

	private static ConcurrentHashMap<Long, StrategyCrowdMap> mapStrategyCrowdMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, StrategyCrowdMap> mapStrategyCrowdMap = new ConcurrentHashMap<>();
	 
	public static StrategyCrowdMap getStrategyCrowdMapByStrategyId(long strategyId) {
		Collection<StrategyCrowdMap> values = mapStrategyCrowdMap.values();
		for (StrategyCrowdMap strategyCrowdMap : values) {
			if(strategyCrowdMap.getStrategyId()==strategyId){
				return BeanUtils.map(strategyCrowdMap, StrategyCrowdMap.class);	
			}
		}
		return null;
	}
	
	private static void loadStrategyCrowdMapAll(StrategyCrowdMap sc) {
		mapStrategyCrowdMapAll.put(sc.getId(), sc);
	}
	
	private static void loadStrategyCrowdMap(StrategyCrowdMap sc) {
		mapStrategyCrowdMap.put(sc.getId(), sc);
	}
	private static void unloadStrategyCrowdMap(StrategyCrowdMap sc) {
		mapStrategyCrowdMap.remove(sc.getId());
	}
	private static ConcurrentHashMap<Long, StrategyDeviceMap> mapStrategyDeviceMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, StrategyDeviceMap> mapStrategyDeviceMap = new ConcurrentHashMap<>();
	
	public static StrategyDeviceMap getStrategyDeviceMapByStrategyId(long strategyId) {
		Collection<StrategyDeviceMap> values = mapStrategyDeviceMap.values();
		for (StrategyDeviceMap strategyDeviceMap : values) {
			if(strategyDeviceMap.getStrategyId()==strategyId){
				return BeanUtils.map(strategyDeviceMap, StrategyDeviceMap.class);	
			}
		}
		return null;
	}
	
	private static void loadStrategyDeviceMapAll(StrategyDeviceMap sd) {
		mapStrategyDeviceMapAll.put(sd.getId(), sd);
	}
	private static void loadStrategyDeviceMap(StrategyDeviceMap sd) {
		mapStrategyDeviceMap.put(sd.getId(), sd);
	}
	private static void unloadStrategyDeviceMap(StrategyDeviceMap sd) {
		mapStrategyDeviceMap.remove(sd.getId());
	}
	
	private static ConcurrentHashMap<Long, StrategyInterestMap> mapStrategyInterestMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, StrategyInterestMap> mapStrategyInterestMap = new ConcurrentHashMap<>();
	private static void loadStrategyInterestMapAll(StrategyInterestMap si) {
		mapStrategyInterestMapAll.put(si.getId(), si);
	}
	private static void loadStrategyInterestMap(StrategyInterestMap si) {
		mapStrategyInterestMap.put(si.getId(), si);
	}
	private static void unloadStrategyInterestMap(StrategyInterestMap si) {
		mapStrategyInterestMap.remove(si.getId());
	}
	
	private static ConcurrentHashMap<Long, StrategyPlatformMap> mapStrategyPlatformMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, StrategyPlatformMap> mapStrategyPlatformMap = new ConcurrentHashMap<>();//有效数据。
	private static ConcurrentHashMap<Integer, CopyOnWriteArraySet<Long>> mapStrategyIdsByPlatformMap = new ConcurrentHashMap<>();//以平台id作为key，value:策略id的set集合。
	
	/**
	 * 
	 * @param sp
	 */
	private static void loadStrategyPlatformMapAll(StrategyPlatformMap sp) {
		mapStrategyPlatformMapAll.put(sp.getId(), sp);
	}
	
	private static void loadStrategyPlatformMap(StrategyPlatformMap sp) {
		mapStrategyIdsByPlatformMap.putIfAbsent(sp.getPlatformId(), new CopyOnWriteArraySet<Long>());
		mapStrategyIdsByPlatformMap.get(sp.getPlatformId()).add(Long.valueOf(sp.getStrategyId()));
		mapStrategyPlatformMap.put(sp.getId(), sp);
	}
	
	private static void unloadStrategyPlatformMap(StrategyPlatformMap sp) {
		mapStrategyIdsByPlatformMap.putIfAbsent(sp.getPlatformId(), new CopyOnWriteArraySet<Long>());
		mapStrategyIdsByPlatformMap.get(sp.getPlatformId()).remove(Long.valueOf(sp.getStrategyId()));
		mapStrategyPlatformMap.remove(sp.getId());
	}
	
	public static List<StrategyPlatformMap> getStrategyPlatformMapsUpdated() {
		return BeanUtils.mapList(lastCtx.getListStrategyPlatformMap(), StrategyPlatformMap.class);
	}
	 
	public static List<StrategyPlatformMap> getStrategyPlatformMapAll() {
		return BeanUtils.mapList(mapStrategyPlatformMapAll.values(), StrategyPlatformMap.class);
	}
	
	private static ConcurrentHashMap<Integer, PlatformTagTemplateMap> mapPlatformTagTemplateMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Integer, PlatformTagTemplateMap> mapPlatformTagTemplateMap = new ConcurrentHashMap<>();//有效数据。
	
	public static List<PlatformTagTemplateMap> getPlatformTagTemplateMapAll() {
		return BeanUtils.mapList(mapPlatformTagTemplateMapAll.values(), PlatformTagTemplateMap.class);
	}
	
	private static void loadPlatformTagTemplateMapAll(PlatformTagTemplateMap ptm) {
		mapPlatformTagTemplateMapAll.put(ptm.getId(), ptm);
	}
	private static void loadPlatformTagTemplateMap(PlatformTagTemplateMap ptm) {
		mapPlatformTagTemplateMap.put(ptm.getId(), ptm);
	}
	private static void unloadPlatformTagTemplateMap(PlatformTagTemplateMap ptm) {
		mapPlatformTagTemplateMap.remove(ptm.getId());
	}
	
	private static ConcurrentHashMap<Integer, PlatformTagSizeMap> mapPlatformTagSizeMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Integer, PlatformTagSizeMap> mapPlatformTagSizeMap = new ConcurrentHashMap<>();//有效数据。
	
	public static List<PlatformTagSizeMap> getPlatformTagSizeMapAll() {
		return BeanUtils.mapList(mapPlatformTagSizeMapAll.values(), PlatformTagSizeMap.class);
	}
	
	private static void loadPlatformTagSizeMapAll(PlatformTagSizeMap psm) {
		mapPlatformTagSizeMapAll.put(psm.getId(), psm);
	}
	private static void loadPlatformTagSizeMap(PlatformTagSizeMap psm) {
		mapPlatformTagSizeMap.put(psm.getId(), psm);
	}
	private static void unloadPlatformTagSizeMap(PlatformTagSizeMap psm) {
		mapPlatformTagSizeMap.remove(psm.getId());
	}
	
	private static ConcurrentHashMap<Long, PlatformAdspaceMap> mapPlatformAdspaceMapAll = new ConcurrentHashMap<>();
	private static ConcurrentHashMap<Long, PlatformAdspaceMap> mapPlatformAdspaceMap = new ConcurrentHashMap<>();//有效数据。
	/**
	 * 获取所有的数据。
	 * @return
	 */
	public static List<PlatformAdspaceMap> getPlatformAdspaceMapAll() {
		return BeanUtils.mapList(mapPlatformAdspaceMapAll.values(), PlatformAdspaceMap.class);
	}
	
	private static void loadPlatformAdspaceMapAll(PlatformAdspaceMap psm) {
		mapPlatformAdspaceMapAll.put(psm.getId(), psm);
	}
	private static void loadPlatformAdspaceMap(PlatformAdspaceMap psm) {
		mapPlatformAdspaceMap.put(psm.getId(), psm);
	}
	private static void unloadPlatformAdspaceMap(PlatformAdspaceMap psm) {
		mapPlatformAdspaceMap.remove(psm.getId());
	}
}
